//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package triger_dao

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao/db_ns"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/project_triger_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type TrigerInfoDbItem struct {
	TrigerId     string `bson:"_id"`
	ProjectId    string `bson:"projectId"`
	TrigerName   string `bson:"trigerName"`
	TypeId       string `bson:"typeId"`
	Secret       string `bson:"secret"`
	CreateTime   int64  `bson:"createTime"`
	CreateUserId string `bson:"createUserId"`
	UpdateTime   int64  `bson:"updateTime"`
	UpdateUserId string `bson:"updateUserId"`
	MsgCount     uint32 `bson:"msgCount"`
}

func (item *TrigerInfoDbItem) ToTrigerInfo() *project_triger_api.TrigerInfo {
	return &project_triger_api.TrigerInfo{
		TrigerId:     item.TrigerId,
		TrigerName:   item.TrigerName,
		TypeId:       item.TypeId,
		Secret:       item.Secret,
		CreateTime:   item.CreateTime,
		CreateUserId: item.CreateUserId,
		UpdateTime:   item.UpdateTime,
		UpdateUserId: item.UpdateUserId,
		MsgCount:     item.MsgCount,
	}
}

type _TrigerInfoDao struct {
	mc *mongo.Collection
}

func newTrigerInfoDao(db *mongo.Database) (*_TrigerInfoDao, error) {
	mc := db.Collection(db_ns.DB_NS_TRIGER + "triger_info")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"projectId", 1}, {"createTime", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_TrigerInfoDao{mc: mc}, nil
}

func (db *_TrigerInfoDao) Insert(ctx context.Context, dbItem *TrigerInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

// projectId为空时表示不检查projectId属性
func (db *_TrigerInfoDao) Exist(ctx context.Context, projectId, trigerId string) (bool, error) {
	query := bson.M{
		"_id": trigerId,
	}
	if projectId != "" {
		query["projectId"] = projectId
	}
	count, err := db.mc.CountDocuments(ctx, query)
	return count > 0, err
}

// projectId为空时表示不检查projectId属性
func (db *_TrigerInfoDao) Get(ctx context.Context, projectId, trigerId string) (*TrigerInfoDbItem, error) {
	query := bson.M{
		"_id": trigerId,
	}
	if projectId != "" {
		query["projectId"] = projectId
	}

	retItem := &TrigerInfoDbItem{}
	err := db.mc.FindOne(ctx, query).Decode(retItem)
	return retItem, err
}

func (db *_TrigerInfoDao) Remove(ctx context.Context, projectId, trigerId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": trigerId, "projectId": projectId})
	return err
}

func (db *_TrigerInfoDao) Count(ctx context.Context, projectId string) (uint32, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"projectId": projectId})
	return uint32(count), err
}

func (db *_TrigerInfoDao) List(ctx context.Context, projectId string, offset, limit int64) ([]*TrigerInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"projectId": projectId}, options.Find().SetSkip(offset).SetLimit(limit).SetSort(bson.M{"createTime": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*TrigerInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_TrigerInfoDao) UpdateName(ctx context.Context, projectId, trigerId, trigerName, updateUserId string, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"projectId": projectId, "_id": trigerId},
		bson.M{"$set": bson.M{
			"trigerName":   trigerName,
			"updateUserId": updateUserId,
			"updateTime":   updateTime,
		}})
	return err
}

func (db *_TrigerInfoDao) UpdateSecret(ctx context.Context, projectId, trigerId, secret, updateUserId string, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"projectId": projectId, "_id": trigerId},
		bson.M{"$set": bson.M{
			"secret":       secret,
			"updateUserId": updateUserId,
			"updateTime":   updateTime,
		}})
	return err
}

func (db *_TrigerInfoDao) UpdateMsgCount(ctx context.Context, projectId, trigerId string, msgCount uint32) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"projectId": projectId, "_id": trigerId},
		bson.M{"$set": bson.M{
			"msgCount": msgCount,
		}})
	return err
}
